<template>
  <div class="bg">
    <!-- 时间搜索框 -->
    <searchTime
      @searchvalue="userSearch"
      @searchreset="searchresets"
      :titMsg="titMsg"
      :selectBoolen="false"
      :selectTime="true"
    />

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="exports" :loading="outExecl">导出数据</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="userName" label="用户昵称" align="center"></el-table-column>
        <el-table-column prop="userPhone" label="手机号码" align="center"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="orderGoldNumber" label="消费（金币）" align="center"></el-table-column>
        <el-table-column prop="userBalance" label="余额（金币）" align="center"></el-table-column>
        <el-table-column prop="orderTime" label="消费时间" align="center"></el-table-column>
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.orderType==1?"充值成功":"消费成功"}}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        style="margin-top:24px"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
import searchTime from "@/components/searchTime";
export default {
  components: {
    searchTime
  },
  data() {
    return {
      // 导出
      outExecl: false,
      titMsg: "用户昵称/手机号码查询/商品名称",
      // options: [
      //   {
      //     value: 1,
      //     label: "充值"
      //   },
      //   {
      //     value: 2,
      //     label: "消费"
      //   }
      // ],
      value: "",
      value1: "",
      downloadLoading: false,
      current: 1, //第一页数据
      tableData: [], //金币订单列表
      total: 0, //总条数
      // 模糊查询
      selectCode: "",
      startDate: "",
      endDate: ""
    };
  },
  mounted() {
    this.ordergoldlist();
  },
  methods: {
    //获取金币订单
    ordergoldlist() {
      let param = {
        size: 10,
        current: this.current,
        selectCode: this.selectCode,
        startDate: this.startDate,
        endDate: this.endDate,
        orderType: 2
      };
      this.$http.article.ordergold(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log(res, "获取金币订单");
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 分页数据
    handleCurrentChange(val) {
      this.current = val;
      this.ordergoldlist();
    },
    // 模拟查询
    userSearch(val) {
       this.current=1;//模糊查询
      this.orderType = val.value;
      this.selectCode = val.search;
      this.startDate = val.firstTime;
      this.endDate = val.endTime;
      this.ordergoldlist();
      console.log("搜索框", val);
    },
    //  重置功能
    searchresets(val) {
      console.log("重置功能", val);
      this.selectCode = "";
      this.startDate = "";
      this.endDate = "";
      if (val === "reseta") this.ordergoldlist();
    },
    // 导出数据
    exports() {
      this.outExecl = true;
      console.log("导出");
      const hader = [
        {
          title: "用户昵称",
          date: "userName"
        },
        {
          title: "手机号码",
          date: "userPhone"
        },
        {
          title: "商品名称",
          date: "goodsName"
        },
        {
          title: "消费（金币）",
          date: "orderGoldNumber"
        },
        {
          title: "余额（金币）",
          date: "userBalance"
        },
        {
          title: "消费时间",
          date: "orderTime"
        },
        {
          title: "订单状态",
          date: "orderType"
        }
      ];
      const excelName = "金币订单列表";
      let param = {
        size: this.total,
        orderType: 2,
        current: 1
      };
      this.$http.article.ordergold(param).then(res => {
        console.log("获取res", res);
        const list = res.result.records.map(item => {
          //因为有枚举类型，所以这里对数据进行处理
          if (item.orderType) {
            item.orderType = item.orderType == 1 ? "充值成功" : "消费成功";
          }
          return item;
        });
        export2Excel(hader, list, excelName);
        setTimeout(() => {
          this.outExecl = false;
        }, 1000);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 10px;
}

.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>